<?php

class Helper_SubNav
{
    public $view;
    
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }

    function subNav()
    {
        $navigation=Zend_Registry::get('navigation');
        $navi=$navigation->toArray();
        
        $active=$this->_findActive($navi,$this->view->url());
        
        if (!$active) return;
        
        $sub=array();
        
        foreach($navi as $nav)
        {
            if ($nav['uri']==$active)
            {
                $sub=$nav['pages'];
                
                break;
            }
        }
        
        if (!empty($sub))
        {
            $submenu = new Zend_Navigation($sub);
            
            return $this->view->navigation()->menu($submenu)->setUlClass('submenu');
        }
    }
    
    protected function _findActive($navi,$current,$depth=0)
    {
        static $active='';
        static $found=false;
        
        foreach($navi as $nav)
        {
            if ($found) break;
            
            if ($depth==0) $active=$nav['uri'];
            
            if ($nav['uri']==$current)
            {
                $found=true;
                break;
            }
            
            if (!empty($nav['pages'])) $this->_findActive($nav['pages'],$current,$depth+1);
        }
        
        return $found ? $active : false;
    }
}
